<template>
  <div class="container">
    <div class="btnBox">
      <a-button htmlType="submit" type="primary" @click="addAndEdit('add')">+添加团队</a-button>
    </div>
    <div class="team_management">
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        bordered
        :rowKey="(record) => record.id"
      >
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="primary" @click="addAndEdit('edit', record)">编辑</a-button>
        </template>
      </s-table>
      <a-modal :title="title" v-model="visible" @ok="handleOk" :maskClosable="false">
        <a-form :form="form" class="permission-form">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队等级">
            <a-input
              v-decorator="[
              'teamLevel',
              {
                rules: [
                  {
                    required: true,
                  },
                ],
                initialValue: title === '添加团队' ? vipLevel : info.teamLevel,
              },
            ]"
              disabled
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="总投资金额">
            <a-input
              v-decorator="[
              'investAmount',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入总投资金额',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.investAmount,
              },
            ]"
              placeholder="请输入总投资金额"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队会员人数">
            <a-input
              v-decorator="[
              'teamPeopleNumber',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团队会员人数',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.teamPeopleNumber,
              },
            ]"
              placeholder="请输入团队会员人数"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队F1人数">
            <a-input
              v-decorator="[
              'oneLeaderNumber',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团队F1人数',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.oneLeaderNumber,
              },
            ]"
              placeholder="请输入团队F1人数"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="每月奖励">
            <a-input
              v-decorator="[
              'monthlyRewards',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入每月奖励',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.monthlyRewards,
              },
            ]"
              placeholder="请输入每月奖励"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团员F1认购收益（%）">
            <a-input
              v-decorator="[
              'firstOneBuyIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团员F1认购收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstOneBuyIncome,
              },
            ]"
              placeholder="请输入团员F1认购收益"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团员F2认购收益（%）">
            <a-input
              v-decorator="[
              'firstTwoBuyIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团员F2认购收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstTwoBuyIncome,
              },
            ]"
              placeholder="请输入团员F2认购收益"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团员F3认购收益（%）">
            <a-input
              v-decorator="[
              'firstThreeBuyIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团员F3认购收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstThreeBuyIncome,
              },
            ]"
              placeholder="请输入团员F3认购收益"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队F1投资收益（%）">
            <a-input
              v-decorator="[
              'firstOneIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团队F1投资收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstOneIncome,
              },
            ]"
              placeholder="请输入团队F1投资收益"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队F2投资收益（%）">
            <a-input
              v-decorator="[
              'firstTwoIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团队F2投资收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstTwoIncome,
              },
            ]"
              placeholder="请输入团队F2投资收益"
            />
          </a-form-item>
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="团队F3投资收益（%）">
            <a-input
              v-decorator="[
              'firstThreeIncome',
              {
                rules: [
                  {
                    required: true,
                    message: '请输入团队F3投资收益',
                  },
                ],
                initialValue: title === '添加团队' ? '' : info.firstThreeIncome,
              },
            ]"
              placeholder="请输入团队F3投资收益"
            />
          </a-form-item>
        </a-form>
      </a-modal>
    </div>
  </div>
</template>

<script>
import STable from '@/components/Table'
import {
  teamManagementList,
  teamManagementDetail,
  teamManagementAdd,
  teamManagementEdit,
} from '@/api/configuration-management'
import { mapGetters } from 'vuex'
// import { common } from '@/common/index'

export default {
  name: 'teamManagement',
  components: {
    STable,
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  data() {
    return {
      form: this.$form.createForm(this),
      formLayout: 'horizontal',
      labelCol: {
        xs: { span: 8 },
        sm: { span: 8 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      visible: false,
      vipLevel: 0,
      title: '',
      info: {},
      columns: [
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center'
        },
        {
          title: '团队等级',
          dataIndex: 'teamLevel',
          align: 'center'
        },
        {
          title: '总投资金额',
          dataIndex: 'investAmount',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '团队会员人数',
          dataIndex: 'teamPeopleNumber',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '团队F1会员人数',
          dataIndex: 'oneLeaderNumber',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '每月奖励',
          dataIndex: 'monthlyRewards',
          customRender: (text) => this.common.numFormat(text),
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          slots: { title: 'action' },
          scopedSlots: { customRender: 'action' },
          align: 'center'
        },
      ],
      // 查询条件参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const { userId } = this.userInfo
        let params = {
          ...parameter,
          userId,
        }
        return new Promise((resolve, reject) => {
          teamManagementList(params)
            .then((res) => {
              if (res.code === 200) {
                this.vipLevel = res.data.total
                resolve(res.data)
              } else {
                this.$message.error(res?.msg)
              }
            })
            .catch((err) => {
              reject(err)
            })
            .finally(() => {})
        })
      },
    }
  },
  methods: {
    addAndEdit(type, record = {}) {
      this.form.resetFields()
      if (type === 'edit') {
        const { userId } = this.userInfo
        const params = {
          teamId: record.teamId,
          userId,
        }
        teamManagementDetail(params).then((res) => {
          if (res.code === 200) {
            this.info = res.data ?? {}
          }
        })
      } else {
        this.info = {}
      }
      this.title = type === 'edit' ? '修改信息' : '添加团队'
      this.visible = true
    },
    refresh(isBackToFirstPage = false) {
      this.$refs.table.refresh(isBackToFirstPage)
    },
    search() {
      this.refresh(false)
    },
    searchReset() {
      this.queryParam = {}
      this.refresh(true)
    },

    handleOk(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          const { userId } = this.userInfo
          console.log(values, 'values')
          let params = {
            teamId: this.info.teamId ?? '',
            firstOneBuyIncome: +values.firstOneBuyIncome,
            firstOneIncome: +values.firstOneIncome,
            firstThreeBuyIncome: +values.firstThreeBuyIncome,
            firstThreeIncome: +values.firstThreeIncome,
            firstTwoBuyIncome: +values.firstTwoBuyIncome,
            firstTwoIncome: +values.firstTwoIncome,
            investAmount: +values.investAmount,
            monthlyRewards: +values.monthlyRewards,
            oneLeaderNumber: +values.oneLeaderNumber,
            teamPeopleNumber: +values.teamPeopleNumber,
            teamLevel: +values.teamLevel,
            userId,
          }
          if (this.title === '添加团队') {
            delete params.teamId
          }
          let url = this.title === '添加团队' ? teamManagementAdd : teamManagementEdit
          url(params).then((res) => {
            if (res.code === 200) {
              this.$message.success(res.msg)
              this.visible = false
              this.info = {}
              this.$refs.table.refresh()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.team_management {
  .delete {
    color: #e94141;
    margin-left: 15px;
  }
  .form {
    display: flex;
    margin-bottom: 20px;
    flex-wrap: wrap;
  }
  .red {
    color: #e94141;
  }
  .green {
    color: #46c16a;
  }
  .permission-form {
    :deep(.permission-group) {
      margin-top: 0;
      margin-bottom: 0;
    }
  }
}
.modal {
  /deep/ .ant-modal {
    width: 1000px !important;
  }
  .user_harder_img {
    display: flex;
    align-items: center;
    margin-bottom: 20px;
    span {
      padding-left: 15px;
    }
    img {
      width: 30px;
      height: 30px;
      border-radius: 50%;
    }
  }
  .user_info {
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;
    border-bottom: 1px solid #f3f4f6;
    &:last-child {
      border-bottom: none;
    }
    .item {
      display: flex;
      div {
        height: 40px;
        width: 50%;
      }
      span {
        &:first-child {
          width: 100px;
          text-align: left;
          padding: 0 15px;
        }
        &:last-child {
          text-align: left;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
      }
      .textarea {
        width: 80%;
      }
    }
  }
}
</style>
